package com.dys.hejiayuncommunity.web.controller.system;

import com.dys.hejiayuncommunity.common.constant.UserConstants;
import com.dys.hejiayuncommunity.common.core.controller.BaseController;
import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.core.page.PageResult;
import com.dys.hejiayuncommunity.common.utils.SecurityUtils;
import com.dys.hejiayuncommunity.system.domain.SysDictType;
import com.dys.hejiayuncommunity.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: SysDictTypeController
 * @Description: 数据字典类型信息
 * @CreateDate: 2024/12/26
 * @Version: v1.0.0
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 分页查询字典类型数据列表
     *
     * @param dictType 字典类型对象
     * @return 字典类型数据列表
     */
    @GetMapping(value = {"/list"})
    public PageResult list(SysDictType dictType) {
        super.startPage();
        List<SysDictType> sysDictTypes = sysDictTypeService.selectDictTypeList(dictType);
        return super.getData(sysDictTypes);
    }

    /**
     * 根据字典类型ID查询字典类型信息
     *
     * @param dictId 字典类型ID
     * @return 响应结果
     */
    @GetMapping(value = {"/{dictId}"})
    public BaseResponse getInfo(@PathVariable Long dictId) {
        return BaseResponse.success(sysDictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型数据
     *
     * @param dictType 字典类型对象
     * @return 响应结果
     */
    @PostMapping
    public BaseResponse add(@RequestBody SysDictType dictType) {
        // 检查字典类型是否唯一
        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(dictType))) {
            return BaseResponse.failed("新增字典"+dictType.getDictName()+"类型失败,字典类型已经存在");
        }
        dictType.setCreateBy(SecurityUtils.getUserName());
        return super.toAjax(sysDictTypeService.insertDictType(dictType));
    }

    /**
     * 修改字典类型数据
     *
     * @param dictType 字典类型对象
     * @return 响应结果
     */
    @PutMapping
    public BaseResponse edit(@RequestBody SysDictType dictType) {
        // 检查字典类型是否唯一
        if (UserConstants.NOT_UNIQUE.equals(sysDictTypeService.checkDictTypeUnique(dictType))) {
            return BaseResponse.failed("修改字典"+dictType.getDictName()+"类型失败,字典类型已经存在");
        }
        dictType.setUpdateBy(SecurityUtils.getUserName());
        return super.toAjax(sysDictTypeService.updateDictType(dictType));
    }

    @DeleteMapping(value = {"/{dictIds}"})
    public BaseResponse remove(@PathVariable Long[] dictIds) {
        return super.toAjax(sysDictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * 清空缓存
     * @return 响应结果
     */
    @DeleteMapping(value = {"/clearCache"})
    public BaseResponse clearCache(){
        sysDictTypeService.clearCache();
        return BaseResponse.success("清除缓存成功");
    }
}
